#include <arpSubControllItem.h>
#include <postManager.h>

namespace View {

  bool ArpAttackButton::OnClicked()
  {
    //    std::cout<<"ArpAttackButton::OnClicked()\n";
    if(true == buttonType && !(arpControll->CheckInputData()) )
      return false;

    if(true == buttonType)
      arpControll->SendAllAddress();

    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_ATTACK_STAT);
    poster->SetDoubleData(Model::ARP_ATTACK);
    poster->SetBoolData(buttonType);
    poster->SendBroadcast();
    return true;
  }

  bool ArpSubControllItem::CheckInputData()
  {
    std::string aIP(gtk_entry_get_text((GtkEntry*)srcIP)),
      aMAC(gtk_entry_get_text((GtkEntry*)srcMAC)),
      bIP(gtk_entry_get_text((GtkEntry*)dstIP)),
      bMAC(gtk_entry_get_text((GtkEntry*)dstMAC));

    NetAddressChecker *checker = new NetAddressChecker();
    ViewConf *conf = ViewConf::GetViewConf();
    MessageDialog *dialog = MessageDialog::GetMessageDialog();
    dialog->SetTitle(conf->GetStringData("arpDialogTitle"));

    if(!checker->CheckIP(aIP) )
      dialog->SetText(conf->GetStringData("arpAIPWrong"));
    else if(!checker->CheckIP(bIP) )
      dialog->SetText(conf->GetStringData("arpBIPWrong"));
    else if(!checker->CheckMAC(aMAC) )
      dialog->SetText(conf->GetStringData("arpAMACWrong"));
    else if(!checker->CheckMAC(bMAC))
      dialog->SetText(conf->GetStringData("arpBMACWrong"));
    else
      return true;
    delete checker;
    dialog->Show();
    return false;
  }

  bool ArpSubControllItem::SendAllAddress()
  {
    std::string aIP(gtk_entry_get_text((GtkEntry*)srcIP)),
      aMAC(gtk_entry_get_text((GtkEntry*)srcMAC)),
      bIP(gtk_entry_get_text((GtkEntry*)dstIP)),
      bMAC(gtk_entry_get_text((GtkEntry*)dstMAC)),
      allAddress(aIP + "#" + aMAC + "#" + bIP + "#" + bMAC);

    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_ARP_ALL_ADDRESS);
    poster->SetStringData(allAddress);
    poster->SendBroadcast();
    return true;
  }

  ArpSubControllItem::ArpSubControllItem(GooCanvasItem *parent)
    :SubControllItem(parent)
  {
    GdkPixbuf *bg,*mg,*fg;
    std::string bgPath,mgPath,fgPath;
    ViewConf *conf = ViewConf::GetViewConf();
    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") + 
      "arpSubItemBg.png";
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    goo_canvas_image_new(root,bg,conf->GetDoubleData("SubControllItemBgX"),
			 conf->GetDoubleData("SubControllItemBgY"),NULL);
    g_object_unref(bg);
    
    srcIP = gtk_entry_new();
    srcMAC = gtk_entry_new();
    dstIP = gtk_entry_new();
    dstMAC = gtk_entry_new();
    attackTimes = gtk_entry_new();
    
    double arpSrcIPX(conf->GetDoubleData("arpSrcIPX")),
      arpSrcIPY(conf->GetDoubleData("arpSrcIPY")),
      arpIPL(conf->GetDoubleData("arpIPL")),
      arpEntryH(conf->GetDoubleData("arpEntryH")),
      arpSrcMACX(conf->GetDoubleData("arpSrcMACX")),
      arpSrcMACY(conf->GetDoubleData("arpSrcMACY")),
      arpMACL(conf->GetDoubleData("arpMACL")),
      arpDstIPX(conf->GetDoubleData("arpDstIPX")),
      arpDstIPY(conf->GetDoubleData("arpDstIPY")),
      arpDstMACX(conf->GetDoubleData("arpDstMACX")),
      arpDstMACY(conf->GetDoubleData("arpDstMACY")),
      arpLabelL(conf->GetDoubleData("arpLabelL"));
    goo_canvas_widget_new(root,srcIP,arpSrcIPX,arpSrcIPY,
			  arpIPL,arpEntryH,NULL);
    goo_canvas_widget_new(root,srcMAC,arpSrcMACX,arpSrcMACY,
			  arpMACL,arpEntryH,NULL);
    goo_canvas_widget_new(root,dstIP,arpDstIPX,arpDstIPY,
                          arpIPL,arpEntryH,NULL);
    goo_canvas_widget_new(root,dstMAC,arpDstMACX,arpDstMACY,
                          arpMACL,arpEntryH,NULL);
    
    pleaseInputArpMsg = 
      goo_canvas_text_new(root,conf->GetStringData("pleaseInputArpMsg").c_str(),
			  conf->GetDoubleData("SubControllItemBgX") + 30,
			  conf->GetDoubleData("SubControllItemBgY") + 20,
			  conf->GetDoubleData("mainWindowLength") - 120,
			  GTK_ANCHOR_NW,
			  "font", "Sans 18","fill_color",
			  "#000000",NULL);

    std::string font("Sans 17");
    std::string color("#0A1CC0");
    AIP = goo_canvas_text_new(root,conf->GetStringData("arpAIP").c_str(),
			      arpSrcIPX - arpLabelL,arpSrcIPY,
			      arpLabelL,GTK_ANCHOR_NW,
			      "font", font.c_str(),"fill_color",
			      color.c_str(),NULL);
    AMAC = goo_canvas_text_new(root,conf->GetStringData("arpAMAC").c_str(),
                              arpSrcMACX - arpLabelL,arpSrcIPY,
			       arpLabelL,GTK_ANCHOR_NW,
			       "font", font.c_str(),"fill_color",
			       color.c_str(),NULL);
    BIP = goo_canvas_text_new(root,conf->GetStringData("arpBIP").c_str(),
                              arpDstIPX - arpLabelL,arpDstIPY,
			      arpLabelL,GTK_ANCHOR_NW,
                              "font", font.c_str(),"fill_color",
                              color.c_str(),NULL);
    BMAC = goo_canvas_text_new(root,conf->GetStringData("arpBMAC").c_str(),
			       arpDstMACX - arpLabelL,arpDstMACY,
			       arpLabelL,GTK_ANCHOR_NW,
			       "font", font.c_str(),"fill_color",
			       color.c_str(),NULL);

    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "starAttackBg.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackMg.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackFg.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    play = new ArpAttackButton(root,conf->GetDoubleData("arpAttackButtonX"),
			       conf->GetDoubleData("arpAttackButtonY"),
			       bg,mg,fg,true,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);

    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "pauseAttackBgpng.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackMgpng.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackFgpng.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    pause = new ArpAttackButton(root,conf->GetDoubleData("arpAttackButtonX"),
				conf->GetDoubleData("arpAttackButtonY"),
				bg,mg,fg,false,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);
    pause->Hide();
  }

  ArpSubControllItem::~ArpSubControllItem()
  {
    //    std::cout<<"ArpSubControllItem::~ArpSubControllItem\n";
    delete pause;
    delete play;
  }

  int ArpSubControllItem::OnReceive(Controller::Broadcast *broadcast)
  {
    //    std::cout<<"ArpSubControllItem::OnReceive\n";
    using namespace Controller;
    BroadcastType type = broadcast->GetBroadcastType();
    switch(type)
    {
      case BROADCAST_TYPE_ATTACK_STAT:{
	if(true == broadcast->GetBoolData() &&
           Model::ARP_ATTACK == broadcast->GetDoubleData()){
	  play->Hide();
	  pause->Show();
	}else if(false == broadcast->GetBoolData() &&
		 Model::ARP_ATTACK == broadcast->GetDoubleData()){
	  pause->Hide();
	  play->Show();
	}
      }break;
    case BROADCAST_TYPE_CHANGE_LANGUAGE:{
      ViewConf *conf = ViewConf::GetViewConf();      
      g_object_set(pleaseInputArpMsg,"text",conf->GetStringData("pleaseInputArpMsg").c_str(),NULL);
      g_object_set(AIP,"text",conf->GetStringData("arpAIP").c_str(),NULL);
      g_object_set(AMAC,"text",conf->GetStringData("arpAMAC").c_str(),NULL);
      g_object_set(BIP,"text",conf->GetStringData("arpBIP").c_str(),NULL);
      g_object_set(BMAC,"text",conf->GetStringData("arpBMAC").c_str(),NULL);
    }break;
    default:return 1;
    }
    return 0;    
  }


}
